//
//  NearbyHospitalController.m
//  HandheldHospital
//
//  Created by zhangchao on 16/2/25.
//  Copyright © 2016年 zhangchao. All rights reserved.
//

#import "NearbyHospitalController.h"
#import "NearbyTableViewCell.h"
#import "SearchTableViewController.h"
#import "AllViewController.h"
#import "DetailsTableViewController.h"
#import "DataManager.h"
#import "Hospital.h"

#define kShared [DataManager defaultManager]

@interface NearbyHospitalController ()

//轮播图数组
@property(nonatomic,strong)NSMutableArray *Figure;

@end


@implementation NearbyHospitalController

//不隐藏tabBar.
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = NO;
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.navigationItem.title = @"附近医院";
    [kShared getHospitalArrayAndRefreshTableView:self.tableView];
    //注册Cell
    [self.tableView registerNib:[UINib nibWithNibName:@"NearbyTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"NearbyCell"];

    //轮播图
    [self Shuffling];
    
}



//轮播图实现
- (void)Shuffling
{
    

    //存放轮播图片数组
    self.Figure = @[@"H1.jpg",@"H2.jpg",@"H3.jpg"].mutableCopy;
    
    UIView *views = [[UIView alloc]init];
    views.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 300);
    
    views.backgroundColor = [UIColor grayColor];
    
    //第三方轮播图实现
    SDCycleScrollView *CycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame: CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 220) imageNamesGroup:self.Figure];
    
    [views addSubview:CycleScrollView];
    
    self.tableView.tableHeaderView = views;
    
    
    //找医院就诊按钮
    UIButton *lookButton = [UIButton buttonWithType:UIButtonTypeSystem];
    lookButton.frame = CGRectMake(0, 220, CGRectGetWidth(self.view.bounds)  * 0.5, 100);
    lookButton.backgroundColor = [UIColor whiteColor];
    
    [lookButton setTitle:@"找医院就诊" forState:UIControlStateNormal];
    
    [lookButton addTarget:self action:@selector(lookAction) forControlEvents:UIControlEventTouchUpInside];
    
    [views addSubview:lookButton];
    
    //搜索附近医院按钮
    UIButton *nearButton = [UIButton buttonWithType:UIButtonTypeSystem];
    nearButton.frame = CGRectMake(CGRectGetWidth(self.view.bounds) * 0.5, 220, CGRectGetWidth(self.view.bounds)  * 0.5, 100);
    nearButton.backgroundColor = [UIColor whiteColor];
    
    [nearButton setTitle:@"搜索附近医院" forState:UIControlStateNormal];
    
    [nearButton addTarget:self action:@selector(nearAction) forControlEvents:UIControlEventTouchUpInside];
    
    [views addSubview:nearButton];
    
    
}
//懒加载
-(NSMutableArray *)Figure{
    if (_Figure == nil) {
        _Figure = [NSMutableArray array];
    }
    return _Figure;
}








- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return kShared.hopitalArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NearbyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NearbyCell" forIndexPath:indexPath];
    Hospital *hospital = kShared.hopitalArray[indexPath.row];
    cell.hospitalLabel.text = hospital.hospitalName;
    cell.numberLabel.text = [NSString stringWithFormat:@"%@ 位医生",hospital.doctorCount];
    cell.levelLabel.text = hospital.hospitalLevel;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 140;
}

/**  获取cell的点击事件  */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailsTableViewController *details = [DetailsTableViewController new];
    details.hospital = kShared.hopitalArray[indexPath.row];
    [self.navigationController pushViewController:details animated:YES];
    
}

    //找医院就诊按钮
- (void)lookAction
{
    AllViewController *all = [AllViewController new];
    [self.navigationController pushViewController:all animated:YES];
}

    //搜索附近医院按钮
- (void)nearAction
{
    SearchTableViewController *search = [[SearchTableViewController alloc] init];
    [self.navigationController pushViewController:search animated:YES];
}

/**   设置头标题  */
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    
    return @"热门医院";
    
}

/**   设置头标题高度    */
- (CGFloat )tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    
    return 31;
    
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
